package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.pojo.AddressBook;
import com.sky.result.Result;
import com.sky.service.user.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "地址簿接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息")
    public Result<List<AddressBook>> findAllList(){
        List<AddressBook> list = addressBookService.list();
        return Result.success(list);
    }

    @PostMapping()
    @ApiOperation("添加当前登录用户的地址信息")
    public Result<String> add(@RequestBody AddressBook addressBook){
        addressBookService.add(addressBook);
        return Result.success();
    }

    @GetMapping("/default")
    @ApiOperation("查询当前登录用户的默认地址")
    public Result<AddressBook> findByDefault(){
        AddressBook addressBook = addressBookService.findByDefault();
        if(addressBook == null){
            return Result.error("没有默认地址");
        }
        return Result.success(addressBook);
    }

    @PutMapping
    @ApiOperation("修改登录用户的地址信息")
    public Result<String> update(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址信息")
    public Result<AddressBook> findById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.findById(id);
        return Result.success(addressBook);
    }

    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result<String> deleteById(Long id){
        addressBookService.deleteById(id);
        return Result.success();
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result<String> setDefaultAddress(@RequestBody AddressBook addressBook){
        addressBookService.setDefaultAddress(addressBook);
        return Result.success();
    }
}
